<?php

namespace App\Http\Controllers\Api\User\Wechat;

use App\Http\Controllers\Controller;
use App\Trip\Wechat\Account\WechatMiniAppLogin;
use App\Trip\Wechat\Account\WechatMIniAppLoginByMobile;
use App\Trip\Wechat\Account\WechatOfficialLogin;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use Exception;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\URL;

class AuthController extends Controller
{
    /**
     * 小程序登录
     * @throws Exception
     */
    public function miniapp_login(Request $request)
    {
        return (new WechatMiniAppLogin($request->all()))->login()->login_success();
    }


    /**
     * 小程序获取手机号
     *
     * @throws Exception
     */
    public function miniapp_mobile(Request $request)
    {
        return (new WechatMIniAppLoginByMobile($request->all()))->login()->login_success();
    }

    /**
     * 公众号网页授权
     * @param Request $request
     * @return Application|ResponseFactory|Response
     */
    public function official_redirect(Request $request)
    {
        $url = URL::to("/api/wechat/official/login");
        return $this->success('', [
            'redirect_url' => (new WechatOfficialLogin())->redirect_url("{$url}"),
        ]);
    }

    /**
     * 公众号登录
     * @param Request $request
     * @return Application|ResponseFactory|Response
     * @throws InvalidArgumentException
     */
    public function official_login(Request $request)
    {
        return (new WechatOfficialLogin())->setCode($request->get('code'))->login()->login_success();
    }
}
